<?php
/**
 * Prado Portal.
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: NewsPortlet.php 369 2010-02-17 12:09:51Z steen.rabol $
 * @package Pradoportal.Porttlets.NewsPortlet
 *
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Common.Data.PortalConfigRecord');
Prado::using('Application.Portlets.NewsPortlet.Common.NewsPortletRecord');

class NewsPortlet extends Portlet
{
	private $_newsconfig	= null;

	public function getNewsConfig()
	{
		return $this->_newsconfig;
	}

	public function setNewsConfig($value)
	{
		$this->_newsconfig	= $value;
	}

	public function onInit($param)
	{
		parent::onInit($param);
		if($this->_newsconfig === null)
		{
			$crow = PortalConfigRecord::finder()->findByname('News_' . $this->Parameters['id']);
			if($crow)
			{
				$this->_newsconfig = @unserialize($crow->value);
			}
		}
	}

	public function dataBind()
	{
		if($this->NewsConfig['showheaders'] == 1)
		{
			$this->pnlNewsHeaders->Visible	= true;
			if($this->NewsConfig['showcount'] > 0)
			{
				$this->NewsHeadersDataList->AllowPaging			= true;
				$this->NewsHeadersDataList->AllowCustomPaging	= true;
				$this->NewsHeadersDataList->PageSize			= $this->NewsConfig['showcount'];
				$this->NewsHeadersDataList->VirtualItemCount 	= $this->DataAccess->createCommand("select count(*) from tblnews where contentid={$this->Parameters['id']} && expire >" . time())->queryScalar();
				$offset	= $this->NewsHeadersDataList->CurrentPageIndex * $this->NewsHeadersDataList->PageSize;
				$limit	= $this->NewsHeadersDataList->PageSize;
				$this->NewsHeadersDataList->DataSource	= $this->DataAccess->createCommand("select * from tblnews where contentid={$this->Parameters['id']} && expire >" . time() . " order by created desc LIMIT $offset,$limit")->query();
			}
			else
			{
				$this->NewsHeadersDataList->DataSource	= $this->DataAccess->createCommand("select * from tblnews where contentid={$this->Parameters['id']} && expire >" . time() . " order by created desc")->query();
			}
			$this->NewsHeadersDataList->dataBind();
		}
		else
		{
			$this->pnlNews->Visible	= true;
			if($this->NewsConfig['showcount'] > 0)
			{
				$this->NewsDataList->AllowPaging		= true;
				$this->NewsDataList->AllowCustomPaging	= true;
				$this->NewsDataList->PageSize			= $this->NewsConfig['showcount'];
				$this->NewsDataList->VirtualItemCount 	= $this->DataAccess->createCommand("select count(*) from tblnews where contentid={$this->Parameters['id']} && expire >" . time())->queryScalar();
				$offset									= $this->NewsDataList->CurrentPageIndex * $this->NewsDataList->PageSize;
				$limit									= $this->NewsDataList->PageSize;
				$this->NewsDataList->DataSource			= $this->DataAccess->createCommand("select * from tblnews where contentid={$this->Parameters['id']} && expire >" . time() . " order by created desc LIMIT $offset,$limit")->query();
			}
			else
			{
				$this->NewsDataList->DataSource	= $this->DataAccess->createCommand("select * from tblnews where contentid={$this->Parameters['id']} && expire >" . time() . " order by created desc")->query();
			}
			$this->NewsDataList->dataBind();
		}
	}

	public function onLoad($param)
	{
		parent::onLoad($param);
		if(!$this->Page->IsPostBack)
		{
			$this->dataBind();
		}
	}

	public function selectNewsHeadLineItem($sender,$param)
	{
		$this->NewsHeadersDataList->EditItemIndex=-1;
		$this->NewsHeadersDataList->SelectedItemIndex = $param->Item->ItemIndex;
		$this->dataBind();
	}

	public function UserName($id)
	{
		return $this->User->Name;
	}

	public function pageNewsHeaderListChanged($sender,$param)
	{
		$this->NewsHeadersDataList->CurrentPageIndex = $param->NewPageIndex;
		$this->dataBind();
	}

	public function pageNewsListChanged($sender,$param)
	{
		$this->NewsDataList->CurrentPageIndex = $param->NewPageIndex;
		$this->dataBind();
	}
}
?>